#include "Frm04.h"

Frm04::Frm04(HmiApp* app, STTWidget *par, const string& id, const string& desc) : STTPage(app->getRuntime(), par, id, desc) {
	hmiApp = app;
}

Frm04::~Frm04() {
}

void Frm04::onInit() {

}

void Frm04::createParts() {
	setMainArg(0, 0, 1024, 600);
	setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 0, 0xc0c0c0);

	/*----------------------------- MButton : wMMButton1                 -----------------------------*/
	wMMButton1 = new STTButton(p_rt, this, "wMMButton1", 1);
	wMMButton1->setMainArg(0, 0, 93, 97, false);
	wMMButton1->setImg("1", "./images/arrow_34_93X97.gray.svg", "");
	wMMButton1->setImg("0", "./images/arrow_34_93X97.svg", "");
	wMMButton1->bindOpenFormHandler(Event::CLICKED, "Frm01", PageMode::NEWWIN, NULL);

	/*----------------------------- List : wMList1                       -----------------------------*/
	wMList1 = new STTList(p_rt, this, "wMList1");
	wMList1->setMainArg(0, 153, 1025, 445, false);
	wMList1->setFonts(0x0, 0xffffff, 16, "文泉驿微米黑", 0);
	wMList1->setMainStyle(10, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0);
	wMList1->setButtonStyleSelected(0, 0x1a2b1, 0xffffff, 0, 0);
	wMList1->onEventHandler(Event::CLICKED, this, (EHandler) &Frm04::wMList1_clk_cb);

	/*----------------------------- Switch : wMSwitch1                   -----------------------------*/
	wMSwitch1 = new STTSwitch(p_rt, this, "wMSwitch1");
	wMSwitch1->setMainArg(519, 12, 80, 49, false, false);
	wMSwitch1->setMainStyle(9999, 0x808080, 0xffffff, 0, 0, 0xc0c0c0, 0, 0x0);
	wMSwitch1->setIndicStyle(9999, 0x1a2b1, 0xffffff, 0, 0, 0x0, 0, 0xffffff);
	wMSwitch1->setKnobStyle(9999, 0x1a2b1, 0xffffff, 0, 0, 0x0, 0, 0xffffff);
	wMSwitch1->onEventHandler(Event::CLICKED, this, (EHandler) &Frm04::wMSwitch1_clk_cb);

	/*----------------------------- Label : wMLabel1                     -----------------------------*/
	wMLabel1 = new STTLabel(p_rt, this, "wMLabel1");
	wMLabel1->setMainArg(515, 61, 88, 25, "单曲循环", TextAlign::CENTER, LabelLongMode::CLIP, 30);
	wMLabel1->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel1->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel1->setFormatText(TextAlign::CENTER, LabelLongMode::CLIP, 30);

	/*----------------------------- Button : wMButton1                   -----------------------------*/
	wMButton1 = new STTButton(p_rt, this, "wMButton1", 0);
	wMButton1->setMainArg(652, 13, 98, 71, false);
	wMButton1->setPressedArg("开始播放", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->setReleasedArg("开始播放", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->onEventHandler(Event::CLICKED, this, (EHandler) &Frm04::wMButton1_clk_cb);

	/*----------------------------- Button : wMButton2                   -----------------------------*/
	wMButton2 = new STTButton(p_rt, this, "wMButton2", 0);
	wMButton2->setMainArg(773, 13, 98, 71, false);
	wMButton2->setPressedArg("暂停", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->setReleasedArg("暂停", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->onEventHandler(Event::CLICKED, this, (EHandler) &Frm04::wMButton2_clk_cb);

	/*----------------------------- Button : wMButton3                   -----------------------------*/
	wMButton3 = new STTButton(p_rt, this, "wMButton3", 0);
	wMButton3->setMainArg(891, 13, 98, 71, false);
	wMButton3->setPressedArg("继续", 0x0, "文泉驿微米黑", 16, 0);
	wMButton3->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton3->setReleasedArg("继续", 0x0, "文泉驿微米黑", 16, 0);
	wMButton3->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton3->onEventHandler(Event::CLICKED, this, (EHandler) &Frm04::wMButton3_clk_cb);

	/*----------------------------- Slider : wMSlider1                   -----------------------------*/
	wMSlider1 = new STTSlider(p_rt, this, "wMSlider1");
	wMSlider1->setMainArg(257, 26, 208, 22, 70, 40, 100, false);
	wMSlider1->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider1->setIndicStyle(9999, 0x1a2b1, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider1->setKnobStyle(7, 0x1a2b1, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);
	wMSlider1->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm04::wMSlider1_vch_cb);

	/*----------------------------- Label : wMLabel2                     -----------------------------*/
	wMLabel2 = new STTLabel(p_rt, this, "wMLabel2");
	wMLabel2->setMainArg(152, 24, 88, 25, "音量", TextAlign::CENTER, LabelLongMode::CLIP, 30);
	wMLabel2->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel2->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel2->setFormatText(TextAlign::CENTER, LabelLongMode::CLIP, 30);

	/*----------------------------- Text : wMText1                       -----------------------------*/
	wMText1 = new STTTextArea(p_rt, this, "wMText1");
	wMText1->setMainArg(99, 110, 630, 44, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText1->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 8, 9, 4, 4, 255);
	wMText1->setPlaceholderText("")->setMaxLength(64)->setAcceptedChars("")->setImeMode(IME::T9);
	wMText1->setFont(0x0, 16, "文泉驿微米黑", 0);

	/*----------------------------- Button : wMButton4                   -----------------------------*/
	wMButton4 = new STTButton(p_rt, this, "wMButton4", 0);
	wMButton4->setMainArg(728, 111, 98, 42, false);
	wMButton4->setPressedArg("添加", 0x0, "文泉驿微米黑", 16, 0);
	wMButton4->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton4->setReleasedArg("添加", 0x0, "文泉驿微米黑", 16, 0);
	wMButton4->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton4->onEventHandler(Event::CLICKED, this, (EHandler) &Frm04::wMButton4_clk_cb);

	/*----------------------------- Button : wMButton5                   -----------------------------*/
	wMButton5 = new STTButton(p_rt, this, "wMButton5", 0);
	wMButton5->setMainArg(827, 111, 98, 42, false);
	wMButton5->setPressedArg("删除", 0x0, "文泉驿微米黑", 16, 0);
	wMButton5->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton5->setReleasedArg("删除", 0x0, "文泉驿微米黑", 16, 0);
	wMButton5->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton5->onEventHandler(Event::CLICKED, this, (EHandler) &Frm04::wMButton5_clk_cb);

	/*----------------------------- Button : wMButton6                   -----------------------------*/
	wMButton6 = new STTButton(p_rt, this, "wMButton6", 0);
	wMButton6->setMainArg(926, 111, 98, 42, false);
	wMButton6->setPressedArg("修改", 0x0, "文泉驿微米黑", 16, 0);
	wMButton6->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton6->setReleasedArg("修改", 0x0, "文泉驿微米黑", 16, 0);
	wMButton6->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton6->onEventHandler(Event::CLICKED, this, (EHandler) &Frm04::wMButton6_clk_cb);

	/*----------------------------- Button : wMButton7                   -----------------------------*/
	wMButton7 = new STTButton(p_rt, this, "wMButton7", 0);
	wMButton7->setMainArg(1, 111, 98, 42, false);
	wMButton7->setPressedArg("清除输入", 0x0, "文泉驿微米黑", 16, 0);
	wMButton7->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton7->setReleasedArg("清除输入", 0x0, "文泉驿微米黑", 16, 0);
	wMButton7->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton7->onEventHandler(Event::CLICKED, this, (EHandler) &Frm04::wMButton7_clk_cb);

	/*----------------------------- Slider : wMSlider2                   -----------------------------*/
	wMSlider2 = new STTSlider(p_rt, this, "wMSlider2");
	wMSlider2->setMainArg(257, 63, 208, 22, 30, 0, 100, false);
	wMSlider2->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider2->setIndicStyle(9999, 0x1a2b1, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider2->setKnobStyle(7, 0x1a2b1, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);
	wMSlider2->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm04::wMSlider2_vch_cb);

	/*----------------------------- Label : wMLabel3                     -----------------------------*/
	wMLabel3 = new STTLabel(p_rt, this, "wMLabel3");
	wMLabel3->setMainArg(152, 61, 88, 25, "语速", TextAlign::CENTER, LabelLongMode::CLIP, 30);
	wMLabel3->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel3->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel3->setFormatText(TextAlign::CENTER, LabelLongMode::CLIP, 30);

	/*called after widget created.*/

	return; //createParts END
}

#include "util/StringUtil.h"
#include "util/FileUtil.h"
#include "util/Util.h"
#include "util/DateUtil.h"

using namespace stt::util;

static vector<string> getData() {
	TTable t = DBMgr::getdb()->query("SELECT V FROM S_CFG WHERE K LIKE 'TTS_%'");
	vector<string> vec;
	t.getColumnValues("V", vec);
	return vec;
}

static void resetData(STTList* wmlist, vector<string> vec) {
	wmlist->clearOptions();

	list<string> ls;
	ls.push_back("DELETE FROM S_CFG WHERE K LIKE 'TTS_%'");
	int i=0;
	for(string v: vec) {
		string k = "TTS_" + to_string(i);
		string sql = R"(INSERT INTO S_CFG (K,V) VALUES ("{0}","{1}");)";
		sql = StringUtil::replace(sql, "{0}", k);
		sql = StringUtil::replace(sql, "{1}", v);
		ls.push_back(sql);
		i++;

		wmlist->addOption(v, v);
	}
	DBMgr::getdb()->execute(ls);
}

static void addData(STTList* wmlist, string s) {
	vector<string> vec = getData();
	vec.push_back(s);
	resetData(wmlist, vec);
}

static void delData(STTList* wmlist, string s) {
	vector<string> vec = getData();
	auto it = std::find(vec.begin(), vec.end(), s);
	if(it!=vec.end()) {
		vec.erase(it);
	}
	resetData(wmlist, vec);
}

static void modifyData(STTList* wmlist, string s) {
	if(wmlist->getSelectedIndex()<0)return;
	vector<string> vec = getData();
	vec[wmlist->getSelectedIndex()] = s;
	resetData(wmlist, vec);
}

void Frm04::onLoad() {
	vector<string> vdata =  getData();
	for(string s: vdata) {
		wMList1->addOption(s, s);
	}
}


bool Frm04::onClosing() {
	return true;
}

void Frm04::onDispose() {

}

static void play(string text, int volume, int playcnt, int speed) {
	getRuntime()->getIVarMgr()->setInt("tts_speed", speed);
	getRuntime()->getIVarMgr()->setInt("tts_volume", volume);
	getRuntime()->getIVarMgr()->setInt("tts_playcnt", playcnt);
	getRuntime()->getIVarMgr()->setChar("tts_text",text);
}

static string getPlayText(bool issingle, STTList* wmlist) {
	string text = "";
	if(issingle) {
		text = wmlist->getSelectedText();
		if(text =="")return "";
	} else {
		for(KV_Str_Str kv: wmlist->getOptions()) {
			text += kv.v + "。";
		}
	}
	return text;
}

void Frm04::wMButton1_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton1（开始播放）的点击事件*/
	int volume = wMSlider1->getValue();
	int playcnt = -1;
	int speed = wMSlider2->getValue();
	string text = getPlayText(wMSwitch1->isOn(),wMList1);
	play(text, volume, playcnt, speed);
}

void Frm04::wMSwitch1_clk_cb(uint16_t code, LvEvent e) {
}

void Frm04::wMButton2_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton2（暂停）的点击事件*/
	play("", 0, 0, 10);
}

void Frm04::wMButton3_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton3（继续）的点击事件*/
}

void Frm04::wMSlider1_vch_cb(uint16_t code, LvEvent e) {
}

void Frm04::wMButton4_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton4（添加）的点击事件*/
	addData(wMList1, wMText1->getText());
}

void Frm04::wMButton5_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton5（删除）的点击事件*/
	delData(wMList1, wMList1->getSelectedText());
}

void Frm04::wMButton6_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton6（修改）的点击事件*/
	modifyData(wMList1, wMText1->getText());
}

void Frm04::wMList1_clk_cb(uint16_t code, LvEvent e) {
	if(wMText1->getText()=="") {
		wMText1->setText(wMList1->getSelectedText());
	}
}

void Frm04::wMButton7_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton7（清除输入）的点击事件*/
	wMText1->setText("");
}

void Frm04::wMSlider2_vch_cb(uint16_t code, LvEvent e) {
}
